# ---------------------------------
# Title: 
#       Appendix Graphs for "Preparing for an Aging Africa: 
#       Data-Driven Priorities for Economic Research and Policy"
# Output: 
#       Colored PNG images for use in presentations, reports, and academic publications.
# Date:
#       December 19, 2024
# ---------------------------------

# ---------------------------------
# Online Appendix Figure A1: Dependency Ratios (over-65)
# ---------------------------------

depratov65 = create_dependency_ratio_plot(
  data = dep_rat_ov65,
  title = "**A.- Dependency Ratio<br>(General Population)**",
  line_sizes = c(1.4, 1.4, 1.4),
  color_scheme = colors_scheme,
  breaks_numer = breaks_numer,
  breaks_text = breaks_text,
  include_country_labels = TRUE
)

oldageratov65 <- create_dependency_ratio_plot(
  data = old_age_rat_ov65,
  title = "**B.- Dependency Ratio<br>(Over 65 Population)**",
  line_sizes = c(0.9, 0.9, 1.6),
  y_labels = c("0", "-", "0.5", "-", "1", "1.25", "1.50"),
  color_scheme = colors_scheme,
  breaks_numer = breaks_numer,
  breaks_text = breaks_text,
  include_country_labels = FALSE
)

com_dep_rat_o65 <-wrap_elements(panel = depratov65 + oldageratov65) +
  labs(tag = "**Year**") +
  theme(
    plot.tag = element_markdown(size = 20, color = blue_colo),
    plot.tag.position = "bottom"
  )

# Save the plot as a PNG file with specified dimensions and resolution
ggsave(
  filename = "dep_rat_o65.png",  # File name for the saved image
  plot = com_dep_rat_o65,
  width = 18,                    # Plot width in inches
  height = 7.5,                  # Plot height in inches
  dpi = 40,                      # Resolution of the saved image (dots per inch)
  device = "png",                # Format of the saved image (PNG)
  path = path_figs,              # Save directory
)

# ---------------------------------
# Online Appendix Figure A3: Employment, Health, & Living Arrangements over the Life 
# Course by Country and Gender WEIGHTED
# ---------------------------------

meas_count_gend(
  weighted = TRUE, 
  breaks_y = break_propo, 
  limits_y = limi_propo, 
  labels_y = label_propo,
  breaks_x = bre_age, 
  labels_x = lab_age,
  data_cols = measures, 
  y_labels = measures_labs, 
  order_first = "pop_row", 
  custom_plots = NULL, 
  themes = list("urban" = theme_bottom())
)

# Save the plot as a PNG file with specified dimensions and resolution
ggsave(
  filename = "out_gen_cou_wgt.png",  # File name for the saved image
  width = 60,                        # Plot width in inches
  height = 60,                       # Plot height in inches
  limitsize = FALSE,
  dpi = 40,                          # Resolution of the saved image (dots per inch)
  device = "png",                    # Format of the saved image (PNG)
  path = path_figs,                  # Save directory
  bg = "white"              
  
)

# ---------------------------------
# Online Appendix Figure A4: Functional
# Limitations over the Life Course by Country and Gender
# ---------------------------------

meas_count_gend(
  weighted = TRUE, 
  breaks_y = break_propo, 
  limits_y = limi_propo, 
  labels_y = label_propo,
  breaks_x = bre_age, 
  labels_x = lab_age,
  data_cols = limitations, 
  y_labels = limit_labs, 
  order_first = "", 
  custom_plots = NULL, 
  themes = list(
    "diff_seei" = theme_top() + theme(
      axis.text.x = element_blank()),
    "diff_comm" = theme_bottom()
  )
)

# Save the plot as a PNG file with specified dimensions and resolution
ggsave(
  filename = "lim_gen_cou_wgt.png", 
  width = 50,                    # Plot width in inches
  height = 35,                   # Plot height in inches
  dpi = 40,                      # Resolution of the saved image (dots per inch)
  device = "png",                # Format of the saved image (PNG)
  limitsize = FALSE,
  path = path_figs,              # Save directory
  bg = "white"              
)

# ---------------------------------
# Online Appendix Figure A5: Employment,
# Health, & Living Arrangements over the Life Course: Aggregate, no weights
# ---------------------------------

country_average(
  data = count_avera(ssa_nwg, measures),
  vars = measures,
  labels = measures_labs_agg,
  forma_theme = theme_top_count(
    size_text=18,
    size_title=20,
    size_legend=20,
    color=blue_colo
  ),
  f_labels_data = meas_labs_text,
  label_conditions = label_measu,  # Pass label conditions
  geom_text_x = "40",  # Custom x position for the text
  geom_text_y = 0.30    # Custom y position for the text
) %>% forma_axes(
  bre_age, 
  lab_age, 
  break_propo, 
  label_propo, 
  limi_propo
)
# Save the plot as a PNG file with specified dimensions and resolution
ggsave(
  filename = "out_cou_nwg.png",  # File name for the saved image
  width = 16.5,                  # Plot width in inches
  height = 18,                   # Plot height in inches
  dpi = 40,                      # Resolution of the saved image (dots per inch)
  device = "png",                # Format of the saved image (PNG)
  path = path_figs,              # Save directory
  bg = "white"              
)


# ---------------------------------
# Online Appendix Figure A6: Employment, 
# Health, & Living Arrangements over the Life Course: By Gender, no weights
# ---------------------------------

country_gender(
  count_gender(gen_nwg, measures),
  measures,
  measures_labs_agg,
  theme_top_count(
    size_text=18,
    size_title=20,
    size_legend=20,
    color=blue_colo
  ),
  vars_with_text=label_measu
) %>% forma_axes(
  bre_age, 
  lab_age, 
  break_propo, 
  label_propo, 
  limi_propo
)
# Save the plot as a PNG file with specified dimensions and resolution
ggsave(
  filename = "out_gen_nwg.png",  # File name for the saved image
  width = 18,                    # Plot width in inches
  height = 18,                   # Plot height in inches
  dpi = 40,                      # Resolution of the saved image (dots per inch)
  device = "png",                # Format of the saved image (PNG)
  path = path_figs,              # Save directory
  bg = "white"              
)

# ---------------------------------
# Online Appendix Figure A7: Employment, 
# Health, & Living Arrangements over the Life Course: by Country and Gender, no weights
# ---------------------------------

meas_count_gend(
  weighted = FALSE, 
  breaks_y = break_propo, 
  limits_y = limi_propo, 
  labels_y = label_propo, 
  breaks_x = bre_age, 
  labels_x = lab_age,
  data_cols = measures, 
  y_labels = measures_labs, 
  order_first = "pop_row", 
  custom_plots = NULL, 
  themes = list("urban" = theme_bottom())
)
# Save the plot as a PNG file with specified dimensions and resolution
ggsave(
  filename = "out_gen_cou_nwg.png",  # File name for the saved image
  width = 60,                    # Plot width in inches
  height = 60,                   # Plot height in inches
  limitsize = FALSE,
  dpi = 40,                      # Resolution of the saved image (dots per inch)
  device = "png",                # Format of the saved image (PNG)
  path = path_figs,              # Save directory
  bg = "white"              
)

# ---------------------------------
# Online Appendix Figure A8: 
# Functional Limitations over the Life Course: Aggregate, no weights
# ---------------------------------

country_average(
  data = count_avera(ssa_nwg, limitations),
  vars = limitations,
  labels = limitations_labs,
  forma_theme = theme_top_count(
    size_text = 18, 
    size_title = 20, 
    size_legend = 20, 
    color = blue_colo
  ),
  f_labels_data = limi_labs_text,
  label_conditions = label_limit,  # Pass label conditions
  geom_text_x = "40",  # Custom x position for the text
  geom_text_y = 0.20    # Custom y position for the text
) %>% forma_axes(
  bre_age, 
  lab_age, 
  break_propo, 
  label_propo, 
  limi_propo
)
# Save the plot as a PNG file with specified dimensions and resolution
ggsave(
  filename = "lim_cou_nwg.png",  # File name for the saved image
  width = 18,                    # Plot width in inches
  height = 10.5,                 # Plot height in inches
  dpi = 40,                      # Resolution of the saved image (dots per inch)
  device = "png",                # Format of the saved image (PNG)
  path = path_figs,              # Save directory
  bg = "white"              
)

# ---------------------------------
# Online Appendix Figure A9: Functional Limitations 
# over the Life Course: By Gender, no weights
# ---------------------------------

country_gender(
  count_gender(gen_nwg, limitations),
  limitations,
  limitations_labs,
  theme_top_count(
    size_text=18,
    size_title=20,
    size_legend=20,
    color=blue_colo
  ),
  vars_with_text = label_limit
) %>% forma_axes(
  bre_age, 
  lab_age, 
  break_propo, 
  label_propo, 
  limi_propo
)
# Save the plot as a PNG file with specified dimensions and resolution
ggsave(
  filename = "lim_gen_nwg.png",  # File name for the saved image
  width = 18,                    # Plot width in inches
  height = 12,                   # Plot height in inches
  dpi = 40,                      # Resolution of the saved image (dots per inch)
  device = "png",                # Format of the saved image (PNG)
  path = path_figs,              # Save directory
  bg = "white"              
)

# ---------------------------------
# Online Appendix Figure A10: Functional 
# Limitations over the Life Course: by Country and Gender, no weights
# ---------------------------------

meas_count_gend(
  weighted = FALSE, 
  breaks_y = break_propo, 
  limits_y = limi_propo, 
  labels_y = label_propo, 
  breaks_x = bre_age, 
  labels_x = lab_age,
  data_cols = limitations, 
  y_labels = limit_labs, 
  order_first = "", 
  custom_plots = NULL, 
  themes = list(
    "diff_seei" = theme_top() + theme(
      axis.text.x = element_blank()),
    "diff_comm" = theme_bottom()
  )
)

# Save the plot as a PNG file with specified dimensions and resolution
ggsave(
  filename = "lim_gen_cou_nwg.png",  # File name for the saved image
  width = 50,                    # Plot width in inches
  height = 35,                   # Plot height in inches
  dpi = 40,                      # Resolution of the saved image (dots per inch)
  device = "png",                # Format of the saved image (PNG)
  limitsize = FALSE,
  path = path_figs,              # Save directory
  bg = "white"              
)